package com.ssyc.gsk_tk.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.view.MyListView;
import com.ssyc.gsk_tk.R;
import com.ssyc.gsk_tk.adapter.ReadChooseAnswerAdapter;
import com.ssyc.gsk_tk.adapter.ReadShowAnswerAdapter;
import com.ssyc.gsk_tk.bean.AnswerInfo;
import com.ssyc.gsk_tk.bean.ReadLvInfo;
import com.ssyc.gsk_tk.util.TkKeys;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * 阅读选择答案的Framgent
 * Created by Administrator on 2018/9/4 0004.
 */

public class TkReadChooseAnswerFragment extends LazyBaseFragment {

    //页面的数据模型
    private AnswerInfo answerInfo;

    //当前题目在大题的位置
    private int currPos;

    //当前题目的的正确答案
    private String rihgtAnswer;

    //是否完成的标识
    private int state;
    /**
     * 选择答案
     */
    private TextView tvQuestion;
    private MyListView lv;


    //展示你的作答,正确答案,每个选项的正确率以及题目解析的相关视图
    //该视图只有在已完成状态下才显示出来
    private LinearLayout llShowAnswer;

    //你的答案
    private TextView tv_own_answer;

    //正确答案
    private TextView tv_right_answer;

    //展示选项正确率的ListView
    private MyListView lv_show_answer;

    //解析
    private TextView tv_parse_answer;

    //答案的数据模型
    private List<ReadLvInfo> oldDatas;

    //展示选择答案列表的适配器
    private ReadChooseAnswerAdapter answerAdapter;

    public static final TkReadChooseAnswerFragment newInstance(AnswerInfo info, int questionNum, int state) {
        TkReadChooseAnswerFragment tkReadChooseAnswerFragment = new TkReadChooseAnswerFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", info);
        bundle.putInt("curr", questionNum);
        bundle.putInt("state", state);
        tkReadChooseAnswerFragment.setArguments(bundle);
        return tkReadChooseAnswerFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tk_read_choose_answer;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);

        //显示内容
        showContent();

        //获取页面数据
        initIntent();

        //初始化视图
        initView(view);

        //设置数据
        initLv();
    }


    /**
     * 获取页面数据
     */
    private void initIntent() {
        answerInfo = (AnswerInfo) getArguments().getSerializable("data");
        //正确答案
        if (null != answerInfo) {
            rihgtAnswer = answerInfo.rightAnswer;
            currPos = getArguments().getInt("curr");
            state = getArguments().getInt("state");
        }
    }

    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        tvQuestion = view.findViewById(R.id.tv_question);
        lv = view.findViewById(R.id.lv);
        tvQuestion.setText(answerInfo.qustionContent);
        llShowAnswer = view.findViewById(R.id.ll_show_answer);
        tv_own_answer = view.findViewById(R.id.tv_own_answer);
        tv_right_answer = view.findViewById(R.id.tv_right_answer);
        lv_show_answer = view.findViewById(R.id.lv_show_answer);
        tv_parse_answer = view.findViewById(R.id.tv_parse_answer);
        if (state == 0) {
            //未完成
            llShowAnswer.setVisibility(View.GONE);
            //未完成列表选项可以点击
            lv.setEnabled(true);
        } else if (state == 1) {
            //以完成
            llShowAnswer.setVisibility(View.VISIBLE);
            //以完成列表选项可以点击
            lv.setEnabled(false);
            //初始化你的作答,正确答案,答案正确率和解析
            initHasCompetedData();
        }
    }

    /**
     * 设置数据
     */
    private void initLv() {
        oldDatas = answerInfo.readLvInfos;
        if (state == 1) {
            //以完成配置正确答案的选中项,这里显示的规则是如果用户选择的答案是正确的,则只将用户选择的答案变色,
            //如果用户选择的答案是错误的,则将用户选择的答案和正确答案分别进行不同对应的颜色进行处理
            for (int i = 0; i < oldDatas.size(); i++) {

                //给每个选项配置一个用户选择的答案
                oldDatas.get(i).chooseAnswer = answerInfo.chooseAnswer;

                //给每个选项配置一个答案
                oldDatas.get(i).rightAnswer = answerInfo.rightAnswer;
            }

            answerAdapter = new ReadChooseAnswerAdapter(1, getContext(), oldDatas, R.layout.tk_lv_read_choose_answer);

            lv.setAdapter(answerAdapter);

        } else if (state == 0) {
            //未完成全部都是未选中状态
            for (int i = 0; i < oldDatas.size(); i++) {
                oldDatas.get(i).isSelected = false;
            }

            answerAdapter = new ReadChooseAnswerAdapter(0, getContext(), oldDatas, R.layout.tk_lv_read_choose_answer);

            lv.setAdapter(answerAdapter);

            //设置条目点击事件
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {

                    if (oldDatas.get(pos).isSelected) {
                        return;
                    } else {
                        //同步数据状态
                        answerAdapter.upDateStete(pos);
                        //保存选择的答案
                        //通过EventBs将用户作答本小题的相关信息传递给TkMainActivity保存
                        //currPos对应的每个大题的小题的位置
                        int choosePos = currPos;
                        //用户选的答案的索引对应ABCDEF
                        String chooseAnswer = oldDatas.get(pos).questionIndex;
                        //是否如正确
                        boolean chooseResult = false;
                        if (rihgtAnswer.equals(chooseAnswer)) {
                            //用户作答正确
                            chooseResult = true;
                        } else {
                            //用户作答错误
                            chooseResult = false;
                        }

                        //回传当前提醒的TypeId,阅读题型对应的是3
                        int typeId = 3;
                        BusInfo busInfo = new BusInfo();
                        busInfo.tkMsg = TkKeys.TKMsg;
                        busInfo.tkChoosePos = choosePos;
                        busInfo.tkChooseAnswer = chooseAnswer;
                        busInfo.tkChooseResult = chooseResult;
                        busInfo.typeId = typeId;
                        EventBus.getDefault().post(busInfo);
                    }
                }
            });
        }
    }


    /**
     * 初始化你的作答,正确答案,答案正确率和解析
     * 需要通过用户是否是已完成和未完成的状态来确定该部分的视图显示与隐藏,同时设置数据的数据源不一样
     * 该设置需要取已完成状态的数据源集合
     */
    private void initHasCompetedData() {

        //显示选项正确率的列表
        List<ReadLvInfo> oldDatas = answerInfo.readLvInfos;
        ReadShowAnswerAdapter answerAdapter = new ReadShowAnswerAdapter(getContext(), oldDatas, R.layout.tk_lv_read_show_answer);
        lv_show_answer.setAdapter(answerAdapter);

        for (int i=0;i<oldDatas.size();i++){
            Log.i("test", answerInfo.readLvInfos.get(i).probability+"");
        }

        //设置你的选项答案,正确答案,和解析
        tv_own_answer.setText(answerInfo.chooseAnswer);

        if(answerInfo.chooseAnswer.equals(answerInfo.rightAnswer)){
            tv_own_answer.setBackgroundResource(R.drawable.tk_tv_right_answer);
        }else{
            tv_own_answer.setBackgroundResource(R.drawable.tk_tv_own_choose_answer);
        }

        tv_right_answer.setText(answerInfo.rightAnswer);

        //设置解析
        tv_parse_answer.setText(answerInfo.explain);

    }
}
